package com.ruoyi.controller;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.service.BaiduService;
import com.ruoyi.utils.SimpleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/baidu")
public class BaiduController {
    @Autowired
    @Resource
    private BaiduService baiduService;

    /**
     * 获取token, 默认的token[ai接口]
     * @return {@code SimpleResult}
     */
    @GetMapping("/get-token")
    public SimpleResult getToken() {
        final Map<String, Object> token = baiduService.getToken();
        if (token == null) {
            return SimpleResult.error("服务器错误");
        }
        return SimpleResult.success(token, "get token successfully");
    }

    /**
     * 语音转文字
     * @param request
     * @param file
     * @return {@code SimpleResult}
     */
    @PostMapping("/speech")
    public SimpleResult speech(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        final Map<String, Object> speech = baiduService.speech(file);
        return SimpleResult.success(speech);
    }


    /**
     * ai对话请求, 参数为json字符串
     * @param requestMessage
     * @return {@code Object}
     */
    @PostMapping("/ai-request")
    public SimpleResult aiRequest(@org.springframework.web.bind.annotation.RequestBody String requestMessage) {
        final SimpleResult simpleResult = baiduService.aiRequest(requestMessage);
        return simpleResult;
    }

    /**
     * ai对话请求, 参数为json字符串
     * @param requestMessage
     * @return {@code Object}
     */
    @PostMapping("/ai-request-devices")
    public SimpleResult aiRequestDevices(@org.springframework.web.bind.annotation.RequestBody String requestMessage) {
        final SimpleResult simpleResult = baiduService.aiRequestDevices(requestMessage);
        return simpleResult;
    }

}
